package com.yellow.routingservice.controller;

import com.yellow.routingservice.common.RouteDTO;
import com.yellow.routingservice.entity.Route;
import com.yellow.routingservice.service.GeocodingService;
import com.yellow.routingservice.service.RouteService;
import jakarta.annotation.Resource;
import org.json.JSONException;
import org.springframework.web.bind.annotation.*;

/**
 * @author: hy
 * @create: 2024-04-14
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/route")
public class RouteController {

    @Resource
    private RouteService routeService;
    @Resource
    private GeocodingService geocodingService;

    @GetMapping("/getRoute/{id}")
    public Route findByOrderId(@PathVariable("id") int orderId){
        return routeService.findByOrderId(orderId);
    }

    @PostMapping("/insertRoute")
    public void insertRoute(@RequestBody RouteDTO routeDTO) throws JSONException {
        System.out.println("进入了insertRoute方法");
        Route route = new Route();
        Integer count = routeService.countRoutes();
        route.setRouteId(count+1);
        route.setOrderId(count+1);
        route.setUserId(routeDTO.getUserId());
        route.setOriginX(String.valueOf(geocodingService.getCoordinates(routeDTO.getStart()).get("longitude")));
        route.setOriginY(String.valueOf(geocodingService.getCoordinates(routeDTO.getStart()).get("latitude")));
        route.setDestinationX(String.valueOf(geocodingService.getCoordinates(routeDTO.getEnd()).get("longitude")));
        route.setDestinationY(String.valueOf(geocodingService.getCoordinates(routeDTO.getEnd()).get("latitude")));
        route.setStart(routeDTO.getStart());
        route.setEnd(routeDTO.getEnd());
        routeService.save(route);
    }
}
